<?php

namespace App\Http\Controllers\Web\Buyer;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\Orders;
use App\Repository\Repositories\Interfaces\OrderProductCommentRepository;
use App\Repository\Repositories\Interfaces\OrderProductsRepository;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use App\Repository\Repositories\Interfaces\UserCommentRepository;
use Illuminate\Http\Request;

/**
 * 订单管理控制器
 *
 * @package App\Http\Controllers\Backend
 */
class CommentsController extends BaseController
{
    /**
     * @var OrderProductCommentRepository
     */
    protected $comments_repo;

    protected $user_id;

    public function __construct(OrderProductCommentRepository $comments_repo)
    {
        parent::__construct();
        $this->comments_repo = $comments_repo;
        $this->user_id = array_get($this->user, 'id');
        //用户只能操作自身所属数据
        $this->comments_repo->scopeQuery(function ($query) {
            return $query->where('user_id', $this->user_id);
        });
    }

    /**
     * 评价列表
     * @return \Illuminate\Support\Collection
     */
    public function index()
    {
        $comments = $this->comments_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $comments, true);
    }



    /**
     * 评价详情
     * @param $id
     * @return \Illuminate\Support\Collection
     */
    public function show($id)
    {
        $comment = $this->comments_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $comment['data']);
    }

    /**
     * 评价创建
     * @param Request $request
     */
    public function store(Request $request, OrderProductsRepository $order_product_repo, ProductsRepository $products_repo, OrdersRepository $orders_repo,  $product_id)
    {
        $this->validate($request, [
            'order_product_id' => 'required',
            'overall' => 'required|in:1,2,3',
            'product_comment' => 'required|max:5|min:1',
            'seller_comment' => 'required|max:5|min:1',
            'logistics_comment' => 'required|max:5|min:1',
            'comment' => 'required|string',
        ]);
        $order_product_id = $request->get('order_product_id');
        $data = $request->all(['overall', 'product_comment', 'seller_comment', 'logistics_comment', 'comment']);

        $order_product = $order_product_repo->find($order_product_id);
        //已评价过的订单商品、不属于用户的商品不能评价
        if($order_product['data']['is_appraise'] == 1 || $order_product['data']['user_id'] != $this->user_id){
            return $this->error(ErrorConst::NO_DATA_CODE_MSG, ErrorConst::NO_DATA_CODE);
        }

        $data['user_id'] = $this->user_id;
        $data['order_code'] = $order_product['data']['order_code'];
        $data['sku_id'] = $order_product['data']['sku_id'];
        $data['product_id'] = $order_product['data']['product_id'];

        \DB::beginTransaction();
        try {
            //创建评论
            $this->comments_repo->create($data);
            //订单商品标记已评论
            $order_product_repo->update(['is_appraise' => 1], $order_product_id);
            //产品评论计数
            $products_repo->incrementCommentCount($product_id, $data['product_comment']);
            //店铺评论计数

            //订单评价是否完成统计
            $order_data = $orders_repo->with(['orderProducts'])->findWhere(['order_code' => $order_product['data']['order_code']]);
            //判断时，由于处于事务中，当前项未被标识未已评价，当仅剩一项时进行标记完成操作
            if($order_data['data'] && ($order = array_get($order_data['data'], 0))){
                $orders_repo->update(['buyer_comment_status' => Orders::BUYER_COMMENT_STATUS_COMPLETED], $order['id']);
            }

            $ret = true;
            \DB::commit();
        } catch (\Exception $e) {
            \DB::rollback(); //事务回滚;
            $ret = false;
        }
//        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $order_data, false);

        return $this->noDataReturn($ret ? true : false);
    }

    /**
     * 买家查看评价--卖家评价
     * @return \Illuminate\Support\Collection
     */
    public function sellerShow(UserCommentRepository $user_comment,$order_code)
    {
        //确保这个订单为该卖家的订单

        $user_comment=$user_comment->scopeQuery(function ($query)use($order_code){
            return $query->where(['order_code' => $order_code]);
        })->all();

        if($user_comment['data']){
            $data['data']=$user_comment['data'][0];
            $data['seller_is_comment']=1;
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $data);
        }else{
            $user_comment['seller_is_comment']=0;
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $user_comment);
        }
    }
}
